package com.music.dao;

import com.laoxuai.beans.annotation.Component;
import com.laoxuai.ibatis.support.BaseDao;
import com.music.pojo.Music;
import com.music.pojo.Playlist;
import com.music.pojo.PlaylistMusic;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

@Component
public class PlaylistDao extends BaseDao<Playlist, Long> {

    @Override
    public int insert(Playlist playlist) throws SQLException {
        String sql = "insert into t_playlist values(?,?,?,?,?,?,?,?,?)";
        return runner.update(sql, playlist.getId(), playlist.getUserId(), playlist.getName(),
                playlist.getDescription(), playlist.getCoverImgUrl(), playlist.getPlayCount(),
                playlist.getStatus(), playlist.getCreateTime(), playlist.getUpdateTime());
    }

    public List<Music> selectMusicById(Long id) throws SQLException {
        String sql = "SELECT * FROM t_music WHERE t_music.`id` IN (SELECT music_id FROM t_playlist_music WHERE playlist_id = ?)";
        return runner.query(sql, new BeanListHandler<>(Music.class), id);
    }

    public void insertPlaylistAndTag(Long id, Integer tagId) throws SQLException {
        String sql = "insert into t_playlist_tag values(null,?,?)";
        runner.update(sql, id, tagId);
    }

    public int insertMusicToPlayList(Long playlistId, Integer id) throws SQLException {
        String sql = "insert into t_playlist_music values(null,?,?)";
        return runner.update(sql,playlistId,id);
    }

    public PlaylistMusic checkMusicFromPlaylist(Long playlistId, Integer id) throws SQLException {
        String sql = "select * from t_playlist_music where playlist_id=? and music_id=?";
        return runner.query(sql,new BeanHandler<>(PlaylistMusic.class),playlistId,id);
    }
}
